package cn.itsource.basic.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IVerifyCodeService;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.vo.PhoneRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    /*获取图形验证码图片*/
    public AjaxResult image(@PathVariable String imageCodeKey) {
        try {
            String base64Str = verifyCodeService.image(imageCodeKey);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (
                BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取图形验证码失败");
        }
    }

    /*获取手机验证码 /verifyCode/sendSmsCode*/
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody PhoneRegisterVo phoneRegisterVo){
        try {
            verifyCodeService.sendSmsCode(phoneRegisterVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取手机验证码失败");
        }
    }
}
